﻿using AjaxControlToolkit;
using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Tim_kiem : System.Web.UI.Page
{

    DigitalWorldDataContext db = new DigitalWorldDataContext();
    string connectionString = WebConfigurationManager.ConnectionStrings["DAL.Properties.Settings.DigitalWorldConnectionString"].ConnectionString;
    static string strConnection = "server =.;database = DigitalWorld;uid=sa;pwd=123";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string tk = Request.QueryString["tk"].ToString();
            loadProduct(tk);
        }
    }

    private void loadProduct(string tk)
    {
        var danhsach = db.products.Where(p => p.name.Contains(tk));
        CollectionPager1.PageSize = 6;
        CollectionPager1.DataSource = danhsach.ToList();
        CollectionPager1.BindToControl = dl_Information;
        dl_Information.DataSource = CollectionPager1.DataSourcePaged;
    }
    protected void dl_Information_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
                    e.Item.ItemType == ListItemType.AlternatingItem)
        {
            PopupControlExtender pce = e.Item.FindControl("PopupControlExtender1") as PopupControlExtender;

            string behaviorID = "pce_" + e.Item.ItemIndex;
            pce.BehaviorID = behaviorID;

            Image img = (Image)e.Item.FindControl("Image1");

            string OnMouseOverScript = string.Format("$find('{0}').showPopup();", behaviorID);
            string OnMouseOutScript = string.Format("$find('{0}').hidePopup();", behaviorID);

            img.Attributes.Add("onmouseover", OnMouseOverScript);
            img.Attributes.Add("onmouseout", OnMouseOutScript);
        }
    }
    [System.Web.Services.WebMethodAttribute(),
  System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        DigitalWorldDataContext db = new DigitalWorldDataContext();
        product product = db.products.SingleOrDefault(p => p.id.Equals(int.Parse(contextKey)));
        manufacturer s = db.manufacturers.SingleOrDefault(s1 => s1.id.Equals(product.providerid));

        StringBuilder b = new StringBuilder();

        b.Append("<table style='background-color:#c1ce56; border: #336699 3px solid; ");
        b.Append("width:350px; font-size:13px; font-family:Arial;' cellspacing='0' cellpadding='3'>");

        b.Append("<tr><td colspan='3' style='background-color:#336699; color:white;'>");
        b.Append("<b>Thông tin sản phẩm</b>");
        b.Append("</td></tr>");
        b.Append("<tr>");
        b.Append("<td style='width:300px;' align='left' colspan='3'><b>Tên sản phẩm : </b><b>" + product.name + "</b></td>");
        b.Append("</tr>");
        b.Append("<tr>");
        b.Append("<td style='width:300px;' align='left' colspan='3'><b>Hãng sản xuất</b> : " + s.companyname + "</td>");
        b.Append("</tr>");
        b.Append("<tr>");
        decimal Price = decimal.Parse(product.price.ToString());
        b.Append("<td style='width:300px;' align='left' colspan='3'><b>Price</b> : " + Price.ToString("#,###,###") + " VNĐ</td>");
        b.Append("</tr>");
        b.Append("<tr>");
        string pathImage = product.photopath;
        string imgName = pathImage.Substring(pathImage.LastIndexOf("/") + 1);
        b.Append("<td valign ='top' style='width:70px;' align='center'>  <img align='left' width='70px' height='70px'  alt='' src='Image/Products/" +
            imgName + "'/></td><td valign ='top' style='width:230px;' colspan='2' align='left'>" + product.description + "</td>");

        b.Append("</tr>");
        b.Append("</table>");

        return b.ToString();
    }
    protected void btntim_Click(object sender, EventArgs e)
    {
        this.loadProduct(txtProductName.Text);
    }
}